package com.example.traffic_learning;

import global.global;
import adapter.MenuAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

	
	// Within which the entire activity is enclosed
	private DrawerLayout mDrawerLayout;

	// ListView represents Navigation Drawer
	private ListView mDrawerList;

	// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
	private ActionBarDrawerToggle mDrawerToggle;

	// Title of the action bar
	private String mTitle = "";

	private Fragment rFragment = null;
	private Intent nextActivity;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		//this.runOnUiThread(updateTimerThread);
		getActionBar().setTitle("Học luật giao thông đường bộ");
		Intent intent = getIntent();
		int pos = 0;
		if(intent != null){
			pos = intent.getIntExtra("possition", 0);
		}
		switch(pos){
		case 1:
			rFragment = new FragLawTrafficLearning();
			break;
		case 2:
			rFragment = new FragStudy();
			break;
		case 3:
			rFragment = new FragTest();
			break;
		case 4:
			rFragment = new FragTrafficSignSystem();
			break;
		case 5:
			rFragment = new FragDetection();
			break;
		case 6:
			rFragment = new FragAbout();
			break;
		}
		
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction ft = fragmentManager.beginTransaction();
		ft.replace(R.id.content_frame, rFragment);
		ft.commit();
		
		mTitle = "Học luật giao thông";
		//getActionBar().setTitle(mTitle);

		// Getting reference to the DrawerLayout
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		//Drawable bg = getResources().getDrawable(R.drawable.background);
		//mDrawerLayout.setBackground(bg);
		mDrawerList = (ListView) findViewById(R.id.drawer_list);
		//mDrawerList.setBackground(bg);
		// Getting reference to the ActionBarDrawerToggle
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.app_name,
				R.string.app_name) {

			/** Called when drawer is closed */
			public void onDrawerClosed(View view) {
				//getActionBar().setTitle(mTitle);
				invalidateOptionsMenu();

			}

			/** Called when a drawer is opened */
			public void onDrawerOpened(View drawerView) {
				//getActionBar().setTitle(mTitle);
				invalidateOptionsMenu();
			}

		};

		// Setting DrawerToggle on DrawerLayout
		mDrawerLayout.setDrawerListener(mDrawerToggle);
		
		ArrayAdapter<String> adapter = new MenuAdapter(this, getResources().getStringArray(R.array.menus));

		// Setting the adapter on mDrawerList
		mDrawerList.setAdapter(adapter);

		// Enabling Home button
		getActionBar().setHomeButtonEnabled(true);

		// Enabling Up navigation
		getActionBar().setDisplayHomeAsUpEnabled(true);

		// Setting item click listener for the listview mDrawerList
		mDrawerList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String[] menuItems = getResources().getStringArray(R.array.menus);
				mTitle = menuItems[position];
				switch(position){
					case 0:
						finish();
						break;
					case 1:
						rFragment = new FragLawTrafficLearning();
						break;
					case 2:
						rFragment = new FragStudy();
						break;
					case 3:
						rFragment = new FragTest();
						break;
					case 4:
						rFragment = new FragTrafficSignSystem();
						break;
					case 5:
						rFragment = new FragDetection();
						break;
					case 6:
						rFragment = new FragAbout();
						break;
				}
				 
				FragmentManager fragmentManager = getFragmentManager();
				FragmentTransaction ft = fragmentManager.beginTransaction();
				ft.replace(R.id.content_frame, rFragment);
				ft.commit();
				mDrawerLayout.closeDrawer(mDrawerList);
			}
		});
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/** Called whenever we call invalidateOptionsMenu() */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// If the drawer is open, hide action items related to the content view
		boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

		menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        try{
        	switch(keyCode)
            {
            case KeyEvent.KEYCODE_CAMERA:
            	FragDetection.startCamera();
            	break;
            }
        }catch(Exception e){}
        return super.onKeyDown(keyCode, event);
    }
	

	/*@Override
	public void onRestart(){
		try{
			FragDetection.onRestartAfterCameraActivity();
		}catch(Exception e){}
		super.onRestart();
	}*/
	@Override
	public void onBackPressed() {
		try{
        	FragTest.showDialog();
        }catch(Exception e){
        	global.isCameraStart = true;
    		super.onBackPressed();
        }
	}
}
